package com.cn.kehong.service;


import com.cn.kehong.api.IProductGzPeriodService;
import com.cn.kehong.dao.LineTaskMapper;
import com.cn.kehong.dao.ProductGzPeriodMapper;
import com.cn.kehong.dao.ProductGzPeriodSlMapper;
import com.cn.kehong.dao.XunjianTaskMapper;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.CollectionsUtil;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProductGzPeriodServiceImpl implements IProductGzPeriodService {

    @Autowired
    private ProductGzPeriodMapper productGzPeriodMapper;

    @Autowired
    private ProductGzPeriodSlMapper productGzPeriodSlMapper;

    @Autowired
    private XunjianTaskMapper xunjianTaskMapper;

    @Autowired
    private LineTaskMapper lineTaskMapper;

    @Override
    public List<ProductGzPeriod> getProductGzPeriodList(QueryForm queryForm) throws Exception {
        int count = productGzPeriodMapper.selectProductProductGzPeriodCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductGzPeriod> productGzPeriods = productGzPeriodMapper.selectProductProductGzPeriods(queryForm);
        productGzPeriods.get(0).setCount(count);
        return productGzPeriods;
    }

    @Override
    public List<ProductGzPeriod> getProductGzPeriodByStatus() throws Exception {
        return productGzPeriodMapper.selectProductGzPeriodByStatus();
    }

    @Override
    public List<ProductGzPeriod> getProductGzPeriodByIds(List<String> ids) throws Exception {
        if(CollectionsUtil.isEmpty(ids)){
            return Lists.newArrayList();
        }
        ProductGzPeriodExample example = new ProductGzPeriodExample();
        example.createCriteria().andIdIn(ids).andStatusEqualTo(Constants.NORMAL_STATUS);
        return productGzPeriodMapper.selectByExample(example);
    }

    @Override
    public String addProductGzPeriod(String peiId,String productCode,String factoryName,String startCode) throws Exception {
        if(StringUtils.isEmpty(peiId)||StringUtils.isEmpty(productCode)||StringUtils.isEmpty(factoryName)){
            throw new Exception("peiId or productCode or factoryName is null");
        }
        ProductGzPeriodExample example = new ProductGzPeriodExample();
        example.createCriteria().andPeiIdEqualTo(peiId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int existCount = productGzPeriodMapper.countByExample(example);
        if(existCount > 0){
            throw new Exception("ProductGzPeriod data has exist");
        }
        ProductGzPeriod productGzPeriod = new ProductGzPeriod();
        String id = CommonUtil.generateRandomNum("gz-");
        productGzPeriod.setId(id);
        productGzPeriod.setFactoryName(factoryName);
        productGzPeriod.setProductCode(productCode);
        productGzPeriod.setStartCode(startCode);
        productGzPeriod.setPeiId(peiId);
        int count = productGzPeriodMapper.insertSelective(productGzPeriod);
        if(count < 1){
            throw new Exception("insert gz data error");
        }
        return "";
    }

    @Override
    public String updateChooseGzGuan(ProductGzPeriod productGzPeriod) throws Exception {
        String id = productGzPeriod.getId();
        if(StringUtils.isEmpty(id)){
            return "id不能为空";
        }
        ProductGzPeriod existProduct = productGzPeriodMapper.selectGzPeriodById(id);
        if(null == existProduct){
            return "干燥阶段不存在";
        }
        if(StringUtils.equals(existProduct.getGzStatus(),"1")){
            return "干燥已经开始";
        }
        Map<String,String> dutyMap = CommonUtil.getOnDutyAndWhiteDay();
        ProductGzPeriodExample example = new ProductGzPeriodExample();
        example.createCriteria().andIdEqualTo(id);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String startTime = simpleDateFormat.format(new Date());
//        productGzPeriod.setStartTime(productGzPeriod.getStartTime());
        productGzPeriod.setGzStatus("1");
        productGzPeriod.setChenpinGuanCode(productGzPeriod.getChengGuanCode());
        int count = productGzPeriodMapper.updateByExampleSelective(productGzPeriod,example);
        if(count > 0){
            XunjianTask xunjianTask = new XunjianTask();
            xunjianTask.setAttr16(id);
            xunjianTask.setFactoryName(existProduct.getFactoryName());
            xunjianTask.setLineType("1");
            xunjianTask.setWorkProcess("干燥");
            xunjianTask.setProductCode(existProduct.getStartCode());
            String banCi = StringUtils.equals(dutyMap.get("whiteDay"), "1") ? "A" : "B";
            xunjianTask.setBanCi(banCi);
            xunjianTask.setTaskName("物料外观");
            xunjianTask.setFrequency("1次/批");
            xunjianTask.setId(CommonUtil.generateRandomNum("xjtask-"));
            count = xunjianTaskMapper.insertSelective(xunjianTask);
            if(count < 1){
                throw new Exception("插入巡检表异常！");
            }

            LineTask lineTask = new LineTask();
            lineTask.setFactoryName(existProduct.getFactoryName());
            lineTask.setLineType("2");
            lineTask.setWorkProcess("干燥");
            lineTask.setProductCode(existProduct.getStartCode());
            lineTask.setBanCi(banCi);
            lineTask.setTaskName("干燥工序记录");
            lineTask.setFrequency("1次/班");
            lineTask.setAttr13(id);
            //干燥机编号
            lineTask.setAttr1(productGzPeriod.getGzGuanCode());
            //开始时间
            lineTask.setAttr2(productGzPeriod.getStartTime());
            //结束时间
            lineTask.setAttr3(productGzPeriod.getEndTime());
            //时长- att4...  att12

            lineTask.setId(CommonUtil.generateRandomNum("linetask-"));

            count = lineTaskMapper.insertSelective(lineTask);
            if(count < 1){
                throw new Exception("插入线边任务异常！");
            }
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateEndGzPeriod(ProductGzPeriod productGzPeriod) throws Exception {
        String id = productGzPeriod.getId();
        if(StringUtils.isEmpty(id)){
            return "id不能为空";
        }
        ProductGzPeriod existProduct = productGzPeriodMapper.selectGzPeriodById(id);
        if(null == existProduct){
            return "干燥阶段不存在";
        }
        if(StringUtils.equals(existProduct.getGzStatus(),"2")){
            return "干燥已经结束";
        }
        ProductGzPeriodExample example = new ProductGzPeriodExample();
        example.createCriteria().andIdEqualTo(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String endTime = simpleDateFormat.format(new Date());
//        productGzPeriod.setEndTime(endTime);
        String startTime = existProduct.getStartTime();
        Date startDate = simpleDateFormat.parse(startTime);
        long spendTime = (new Date().getTime() - startDate.getTime())/1000/60;
        productGzPeriod.setSpendTime(spendTime+"");
        productGzPeriod.setGzStatus("2");
        int count = productGzPeriodMapper.updateByExampleSelective(productGzPeriod,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateGzJinLiaoPeriod(String peiId) throws Exception {
        ProductGzPeriod update = new ProductGzPeriod();
        update.setEnterYaol("1");
        ProductGzPeriodExample example = new ProductGzPeriodExample();
        example.createCriteria().andPeiIdEqualTo(peiId).andStatusEqualTo(Constants.NORMAL_STATUS);
        int count = productGzPeriodMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateBatchProductGzPeriod(List<ProductGzPeriod> productGzPeriods) throws Exception {
        if(CollectionUtils.isEmpty(productGzPeriods)){
            return "数组为空";
        }
        int count = productGzPeriodMapper.batchUpdateGzPeriods(productGzPeriods);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateBatchProductGzSl(List<ProductGzPeriodSl> productGzPeriodSls) throws Exception {
        if(CollectionUtils.isEmpty(productGzPeriodSls)){
            return "数组为空";
        }
        int count = productGzPeriodSlMapper.updateBatchProductGzSl(productGzPeriodSls);
        return CommonUtil.outStr(count);
    }

    @Override
    public String insertBatchProductGzSl(List<ProductGzPeriodSl> productGzPeriodSls) throws Exception {
        if(CollectionUtils.isEmpty(productGzPeriodSls)){
            return "数组为空";
        }
        int count = productGzPeriodSlMapper.insertBatchProductGzSl(productGzPeriodSls);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ProductGzPeriod> getChengpinList(String factoryName) throws Exception {
        List<ProductGzPeriod> productGzPeriods = productGzPeriodMapper.selectChengpinList(factoryName);
        return productGzPeriods;
    }

    @Override
    public List<ProductGzPeriod> getProductGzPeriodBackList(QueryForm queryForm) throws Exception {
        int count = productGzPeriodMapper.getProductGzPeriodBackCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<ProductGzPeriod> productGzPeriods = productGzPeriodMapper.getProductGzPeriodBackList(queryForm);
        productGzPeriods.get(0).setCount(count);
        return productGzPeriods;
    }
}
